import { fileURLToPath, URL } from 'node:url'

import {build, defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from "@vitejs/plugin-vue-jsx"
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import topLevelAwait from 'vite-plugin-top-level-await'
import viteCompression from 'vite-plugin-compression';

// https://vitejs.dev/config/
export default defineConfig(({ command, mode, ssrBuild }) =>{
  const {
    VITE_APP_PORT,
    VITE_APP_OPEN,
    VITE_APP_API,
    VITE_APP_API_TARGET,
    VITE_BASE_URL,
    VITE_AUDIO_CC1_URL,
    VITE_AUDIO_CC1_API,
    VITE_AUDIO_CC2_URL,
    VITE_AUDIO_CC2_API,
    VITE_AUDIO_TNCs_URL,
    VITE_AUDIO_TNCs_API,
    VITE_AUDIO_Test_URL,
    VITE_AUDIO_Test_API
  } = loadEnv(mode, process.cwd())
  console.log('vite.config',VITE_APP_PORT)
  return {
    base: './',
    plugins: [
      vue(),
      vueJsx(),
      createSvgIconsPlugin({
        // 指定要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // 执行icon name的格式
        symbolId: 'icon-[name]',
        // svgoOptions: {
        //   plugins: [
        //     {
        //       name: 'removeAttrs',
        //       params: { attrs: ['class', 'data-name', 'fill', 'stroke'] }
        //     }
        //   ]
        // }
      }),
      topLevelAwait({
        promiseExportName: '__tla',
        promiseImportName: i => `__tla_${i}`
      }),
      viteCompression(
          {
            algorithm: 'gzip',
            threshold: 10240,
            verbose: false,
            deleteOriginFile: false
          }
      )
    ],
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              }
            }
          }
        ],
      },
    },
    build: {
      chunkSizeWarningLimit: 1500, // 分块打包，分解块，将大块分解成更小的块
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString()
            }
          }
        }
      }
    },
    server: {
      host: '0.0.0.0',
      port: Number(VITE_APP_PORT),
      strictPort: true, // 端口被占用直接退出
      open: Boolean(VITE_APP_OPEN),// 在开发服务器启动时自动在浏览器中打开应用程序
      proxy: { // 代理配置
        [VITE_APP_API]: {
          target: VITE_APP_API_TARGET,
          // target: 'http://cqiae-test.dk-aicall.com',
          changeOrigin: true, // 跨域配置
          rewrite: (path) => path.replace(new RegExp(VITE_APP_API), '')
        },
        [VITE_AUDIO_CC1_API]: {
          target: VITE_AUDIO_CC1_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(VITE_AUDIO_CC1_API), '')
        },
        [VITE_AUDIO_CC2_API]: {
          target: VITE_AUDIO_CC2_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(VITE_AUDIO_CC2_API), '')
        },
        [VITE_AUDIO_TNCs_API]: {
          target: VITE_AUDIO_TNCs_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(VITE_AUDIO_TNCs_API), '')
        },
        [VITE_AUDIO_Test_API]: {
          target: VITE_AUDIO_Test_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(VITE_AUDIO_Test_API), '')
        }
      }
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    }
  }
})
